package day8.part1;

/**
 * @Author juzipi
 * @Date 2021/6/18 10:21
 * @Info
 */
public class StudentTest {

    public static void main(String[] args) {
        Student[] studnets = new Student[20];
        for (int i = 0; i < studnets.length; i++) {
            studnets[i] = new Student();
            studnets[i].number = i+1;
            //自定义区间（默认左闭右开）[a,b]，(b-a+1)+a --->
            //这个区间就是 [1,6]
            studnets[i].state = (int) (Math.random()*(6-1+1)+1);
            studnets[i].score = (int) (Math.random()*(100+1));
        }
        for (Student studnet : studnets) {
            System.out.println(studnet);
            if (studnet.state == 3) {
                System.out.println("===========================");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("---->>>>>>>>>" + studnet);
            }

        }

        for (int i = 0; i < studnets.length -1; i++) {
            for (int j = 0; j < studnets.length - 1 -i; j++) {
                if (studnets[j].score > studnets[j +1].score){
                    //注意换顺序交换的是对象
                    Student studnet = studnets[j];
                    studnets[j] = studnets[j + 1];
                    studnets[j + 1] = studnet;
                }
            }
        }

        System.out.println("_+++++++++++++++++++++++++++++++");
        for (Student s : studnets) {
            System.out.println(s+"                ");
        }

        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        StudentTest studentTest = new StudentTest();
        studentTest.printStudent(studnets);
        studentTest.searchState(studnets,4);
        studentTest.sort(studnets);

    }

    //遍历数组
    public void printStudent(Student[] students){
        for (Student student : students) {
            System.out.println(student);
        }
    }

    //查找数组中指定年级的学生
    public void searchState(Student[] students,int state){
        for (Student student : students) {
            if (student.state == state) {
                System.out.println(student);
            }
        }
    }

    //排序
    public void sort(Student[] students){
        for (int i = 0; i < students.length -1; i++) {
            for (int j = 0; j < students.length - 1 -i; j++) {
                if (students[j].score > students[j +1].score){
                    //注意换顺序交换的是对象
                    Student studnet = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = studnet;
                }
            }
        }
    }

}

class Student{
    int number;
    int state;
    int score;

    @Override
    public String toString() {
        return "Student{" +
                "number=" + number +
                ", state=" + state +
                ", score=" + score +
                '}';
    }
}

class Teacher{
    int number;
    int state;
    int score;

    @Override
    public String toString() {
        return "Student{" +
                "number=" + number +
                ", state=" + state +
                ", score=" + score +
                '}';
    }
}
